package com.gaotu.demo.transaction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

@Configuration
public class TransactionManagerConfiguration {
    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public JpaTransactionManager jpaTransactionManager(EntityManagerFactory entityManagerFactory){
        return new JpaTransactionManager(entityManagerFactory);
    }

    @Bean
    public DataSourceTransactionManager sqlSessionTransactionManager(DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public TransactionTemplate dataSourceTransactionTemplate(@Autowired @Qualifier("dataSourceTransactionManager")DataSourceTransactionManager transactionManager){
        return new TransactionTemplate(transactionManager);
    }

    @Bean
    public TransactionTemplate jpaTransactionTemplate(JpaTransactionManager transactionManager){
        return new TransactionTemplate(transactionManager);
    }

    @Bean
    public TransactionTemplate sqlSessionTransactionTemplate(@Autowired @Qualifier("sqlSessionTransactionManager") DataSourceTransactionManager sqlSessionTransactionManager){
        return new TransactionTemplate(sqlSessionTransactionManager);
    }

}
